package lg.minecraft.plugin.pokemonmenu.command;

import lg.minecraft.plugin.library.command.CommandContext;
import lg.minecraft.plugin.library.command.CommandType;
import lg.minecraft.plugin.library.command.SubCommand;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.gui.PartyInventor;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class SubOpenCommand extends SubCommand {

    public SubOpenCommand() {
        super("open", "o");
    }

    @Override
    public String getPermission() {
        return "LGPokemonMenu.command.open";
    }

    @Override
    public String getDescription() {
        return "打开界面";
    }

    @Override
    public CommandType getCommandType() {
        return CommandType.PLAYER;
    }

    @Override
    public void onCommand(CommandContext context) {
        Player player = context.getPlayer();
        OfflinePlayer target;
        if (context.getArgLength() == 0) {
            target = player;
        } else {
            if (!player.hasPermission("LGPokemonMenu.command.view.other")) {
                player.sendMessage(LGPokemonMenu.getMessage().getNoPermission());
                return;
            }
            String playerName = context.getArg(0);
            target = Bukkit.getPlayer(playerName);
            if (target == null) {
                for (OfflinePlayer temp : Bukkit.getOfflinePlayers()) {
                    String name = temp.getName();
                    if (name != null && name.toLowerCase().contains(playerName.toLowerCase())) {
                        target = temp;
                        break;
                    }
                }
            }
            if (target == null) {
                player.sendMessage("玩家不存在");
                return;
            }
        }
        new PartyInventor(target.getUniqueId()).openInventory(player);
    }

    @Override
    public List<String> onTabComplete(CommandContext context) {
        if (context.getArgLength() == 1) {
            return tabValuePlayerName(context.getArg(0));
        }
        return null;
    }

}
